/**
 * Created by yihua on 2017/2/7.
 */
var addbrandUrl = '';
$(document).ready(function () {
    //init();
    $("#toolbar-add").unbind('click').click(function () {
        $("#myModal").modal("show");
        init()
    });
    addBrand();
});

function init() {
    $("#logo").fileinput({
        uploadUrl: "/common/uploadimage", // server upload action
        uploadAsync: true,
        showPreview: false,
        showUpload: false,
        allowedFileExtensions: ['jpg', 'png', 'gif'],
        maxFileCount: 5,
    }).on('fileuploaded', function (event, data, id, index) {
        var ret = data.response;
        if (0 != ret.errno) {
            toastr.error('上传失败', '', {positionClass: 'toast-bottom-center'});
        }
        toastr.success('上传成功', '', {positionClass: 'toast-bottom-center'});
        console.log(data.response);
        addbrandUrl = ret.data.url;
    }).on("filebatchselected", function (event, files) {
        $("#logo").fileinput("upload");
    });
}

function addBrand() {
    var btn = $("#confirm");
    btn.unbind('click').click(function () {
        var options = {
            url: '/home/shop/addbrand',
            beforeSubmit: addvalidate,  // pre-submit callback
            success: addshowResponse,  // post-submit callback
            dataType: 'json',
            type: 'post',
            data: {
                'logo': addbrandUrl,
            }
        };
        $("#brandform").ajaxSubmit(options)
    });
}


function addvalidate(formData, jqForm, options) {
    for (var i = 0; i < formData.length; i++) {
        //console.log(formData[i].name)
        if (formData[i].name == 'file') {
            break;
        }
        if (!formData[i].value) {
            toastr.error('请输入所有选项', '', {positionClass: 'toast-bottom-center'});
            return false;
        }
    }

    console.log(formData)
}

function addshowResponse(responseText, statusText, xhr, $form) {
    console.log(responseText);
    if (0 !== responseText.errno) {
        toastr.error(responseText.errmsg, '', {positionClass: 'toast-bottom-center'});
    } else {
        toastr.success('成功', '', {positionClass: 'toast-bottom-center'});
        $("#myModal").modal("hide");
        console.log('========================')
        window.location.reload();
    }
}

/*
 function addBrand(){
 var btn = $("#confirm");
 btn.click(function(){

 var flag = true;
 $("#addbrand").find("input[type='text']").each(function () {
 console.log($(this).val());
 if ($(this).val() == "") {
 flag = false;
 return false;
 }
 })
 if(false === flag){
 //toastr.error('请输入增加优惠券所需要的全部信息');
 toastr.error('请输入增加优惠券所需要的全部信息','',{positionClass: 'toast-bottom-center'});
 return false;
 }

 var brandName = $('#brand_name').val();
 var isShow = $('#is_show').val();
 var displayOrder = $('#display_order').val();
 //var logoFile = $('#logo').val();
 var myData = new FormData();
 myData.append('brand_name',brandName);
 myData.append('is_show',isShow);
 myData.append('display_order',displayOrder);
 myData.append('logo',addbrandUrl);
 console.log(myData);
 console.log('----------------');
 return false;
 $.ajax({
 url : '/home/shop/addbrand',
 timeout : 100000,
 type : 'post',
 processData: false,
 contentType: false,
 dataType : 'json',
 data : myData,
 success : function (rs) {
 if (rs.errno != 0) {
 toastr.error(rs.errmsg,'',{positionClass: 'toast-bottom-center'});
 }
 if (rs.errno == 0){
 toastr.success(rs.errmsg);
 window.location.reload();
 $('#myModal').modal('hide');
 }
 },
 error : function () {
 toastr.error('系统错误','',{positionClass: 'toast-bottom-center'});
 }
 });

 });
 }
 */
/**
 $(document).ready(function() {

    $("#logo").fileinput({
        showCaption: false,
        showUpload : false,
        showRemove : false,
    });
    var btn = $("#confirm");
    btn.click(function(){
        var flag = true;
        $("#addbrand").find("input[type='text']").each(function () {
            console.log($(this).val());
            if ($(this).val() == "") {
                flag = false;
                return false;
            }
        })
        if(false === flag){
            //toastr.error('请输入增加优惠券所需要的全部信息');
            toastr.error('请输入增加优惠券所需要的全部信息','',{positionClass: 'toast-bottom-center'});
            return false;
        }
        var brandName = $('#brand_name').val();
        var isShow = $('#is_show').val();
        var displayOrder = $('#display_order').val();
        //var logoFile = $('#logo').val();
        var myData = new FormData();
        myData.append('brand_name',brandName);
        myData.append('is_show',isShow);
        myData.append('display_order',displayOrder);
        myData.append('logo',$("#logo")[0]);
        console.log(myData);

        $.ajax({
            url : '/home/shop/addbrand',
            timeout : 100000,
            type : 'post',
            processData: false,
            contentType: false,
            dataType : 'json',
            data : myData,
            success : function (rs) {
                if (rs.errno != 0) {
                    toastr.error(rs.errmsg,'',{positionClass: 'toast-bottom-center'});
                }
                if (rs.errno == 0){
                    toastr.success(rs.errmsg);
                    window.location.reload();
                    $('#myModal').modal('hide');
                }
            },
            error : function () {
                toastr.error('系统错误','',{positionClass: 'toast-bottom-center'});
            }
        });
        //alert(test);
    });

});

 **/